var BrowserDetect = {
	support3d: function() {
		//if($.browser.mozilla) return false;
		
		return $('html').hasClass('csstransforms3d');
	},

	supportTransition: function() {
		return $('html').hasClass('csstransitions');
	}	
};

var Utility = {
	time_formats: [
	    
	],
	liveSite: '',

	log: function(msg) {
		if(window.location.hostname.search(Utility.liveSite) === -1 && typeof console == 'object') {
			console.log(msg);
		}
	},

	truncateByWords: function(sentence, num, makeUrls) {
		var result = sentence;
		if(typeof result == 'undefined' ) {
			return '';
		}
		var resultArray = result.split(" ");

		if(resultArray.length > num) {
			resultArray = resultArray.slice(0, num);

			if(resultArray[resultArray.length - 1].length == 1) {
				resultArray.pop();	
			}

			result = resultArray.join(" ") + "...";
		}
		
		if(makeUrls != undefined && makeUrls == false) {
			return result;
		} else {
			return Utility.concatUrlFromText(result);
		}
	},

	truncateByChars: function(text, num) {
		var result = text;

		if(result.length > num) {
			var newString = result.substring(0, num);
			//newString = newString.replace(/\w+$/, '');
			//newString = newString.substring(0, newString.length - 1);
			newString += '...';
			
			return newString;
		}
		
		return result;
	},

	concatUrlFromText: function(text) {
		var arr = text.match(/(\b(https?|ftp|file):\/\/[\-A-Z0-9+&@#\/%?=~_|!:,.;]*[\-A-Z0-9+&@#\/%=~_|])/ig);
		var newText = text;

		if(arr != null) {
			for(var i=0; i<arr.length; i++) {
				var res = arr[i];
				var domain = '';
				
				if(res.indexOf('http://www.') > -1) {
					domain = res.substring(11, res.indexOf('/', 12));
				} else if(res.indexOf('http://') > -1) {
					domain = res.substring(7, res.indexOf('/', 8));
				}
				
			    newText = newText.replace(res, '<a href="'+res+'" title="'+res+'">['+Utility.truncateByChars(domain, 15)+']</a>');
			}
		    
		    return newText;
		}
		
		return text;
	},

	concatUrlFromAnchorTags: function(tags) {
		$.each(tags, function(k, v) {
			var $tag = $(v);
			if(!$tag.hasClass('hashtag') && !$tag.hasClass('username') && !$tag.hasClass('item-read-more')) {
				var href = $tag.attr('href');
				var domain = '';

				if(href.indexOf('http://www.') > -1) {
					if(href.indexOf('/', 12) > -1) {
						domain = href.substring(11, href.indexOf('/', 12));
					} else {
						domain = href.substring(11);
					}
				} else if(href.indexOf('http://') > -1) {
					if(href.indexOf('/', 8) > -1) {
						domain = href.substring(7, href.indexOf('/', 8));
					} else {
						domain = href.substring(7);
					}
				} else {
					domain = 'Read more';
				}
				
				$tag.html('['+domain+']');
			}
		});
	},

	urlEncode: function(url) {
		var encodedUrl = encodeURIComponent(url);
		
		return encodedUrl;
	},

	prettyDate: function(date, compareTo) {
	    if(!date) {
	        return;
	    }

	    var lang = {
	            
	        },
	        formats = [
	            
	        ],
	        isString = typeof date == 'string',
	        date = isString ?
	                    new Date(('' + date).replace(/-/g,"/").replace(/[TZ]/g," ")) :
	                    date,
	        compareTo = compareTo || new Date,
	        seconds = (compareTo - date +
	                        (compareTo.getTimezoneOffset() -
	                            
	                            (isString ? 0 : date.getTimezoneOffset())
	                        ) * 60000
	                    ) / 1000,
	        token;

	    if(seconds < 0) {
	        seconds = Math.abs(seconds);
	        token = lang.from ? ' ' + lang.from : '';
	    } else {
	        token = lang.ago ? ' ' + lang.ago : '';
	    }

	    function normalize(val, single) {
	        var margin = 0.1;
	        if(val >= single && val <= single * (1+margin)) {
	            return single;
	        }
	        return val;
	    }

	    for(var i = 0, format = formats[0]; formats[i]; format = formats[++i]) {
	        if(seconds < format[0]) {
	            if(i === 0) {
	                // Now
	                return format[1];
	            }

	            var val = Math.ceil(normalize(seconds, format[3]) / (format[3]));
	            return val +
	                    ' ' +
	                    (val != 1 ? format[2] : format[1]) +
	                    (i > 0 ? token : '');
	        }
	    }
	},

	stripTags: function(input, allowed) {
	    allowed = (((allowed || "") + "").toLowerCase().match(/<[a-z][a-z0-9]*>/g) || []).join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
	    var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
	        commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
	    return input.replace(commentsAndPhpTags, '').replace(tags, function ($0, $1) {
	        return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
	    });
	},

	getTextMonth: function(i, shortened) {
		var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
		return (shortened == true) ? months[i].substring(0, 3) : months[i];
	},

	thisMovie: function(movieName) {
		if (navigator.appName.indexOf("Microsoft") != -1) {
			return window[movieName];
		} else {
			return document[movieName];
		}
	},

	
	gaTrackEvent: function(category, action, label) {
		//Utility.log("GA TRACKING: "+category +"|" +action+"|"+label);
		// KEY
		/* 
		
										
		*/
		
		_gaq.push(['_trackEvent', category, action, label]);
	}, 

	cbSplit: function (str, separator, limit) {
	    // if `separator` is not a regex, use the native `split`
	    if (Object.prototype.toString.call(separator) !== "[object RegExp]") {
	        return str.split(separator, limit);
	    }

	    var output = [],
	        lastLastIndex = 0,
	        flags = (separator.ignoreCase ? "i" : "") +
	                (separator.multiline  ? "m" : "") +
	                (separator.sticky     ? "y" : ""),
	        separator = RegExp(separator.source, flags + "g"), // make `global` and avoid `lastIndex` issues by working with a copy
	        separator2, match, lastIndex, lastLength;

	    str = str + ""; // type conversion
	    if (!this.cbSplit._compliantExecNpcg) {
	        separator2 = RegExp("^" + separator.source + "$(?!\\s)", flags); // doesn't need /g or /y, but they don't hurt
	    }

	    /* behavior for `limit`: if it's...
	    - `undefined`: no limit.
	    - `NaN` or zero: return an empty array.
	    - a positive number: use `Math.floor(limit)`.
	    - a negative number: no limit.
	    - other: type-convert, then use the above rules. */
	    if (limit === undefined || +limit < 0) {
	        limit = Infinity;
	    } else {
	        limit = Math.floor(+limit);
	        if (!limit) {
	            return [];
	        }
	    }

	    while (match = separator.exec(str)) {
	        lastIndex = match.index + match[0].length; // `separator.lastIndex` is not reliable cross-browser

	        if (lastIndex > lastLastIndex) {
	            output.push(str.slice(lastLastIndex, match.index));

	            // fix browsers whose `exec` methods don't consistently return `undefined` for nonparticipating capturing groups
	            if (!this.cbSplit._compliantExecNpcg && match.length > 1) {
	                match[0].replace(separator2, function () {
	                    for (var i = 1; i < arguments.length - 2; i++) {
	                        if (arguments[i] === undefined) {
	                            match[i] = undefined;
	                        }
	                    }
	                });
	            }

	            if (match.length > 1 && match.index < str.length) {
	                Array.prototype.push.apply(output, match.slice(1));
	            }

	            lastLength = match[0].length;
	            lastLastIndex = lastIndex;

	            if (output.length >= limit) {
	                break;
	            }
	        }

	        if (separator.lastIndex === match.index) {
	            separator.lastIndex++; // avoid an infinite loop
	        }
	    }

	    if (lastLastIndex === str.length) {
	        if (lastLength || !separator.test("")) {
	            output.push("");
	        }
	    } else {
	        output.push(str.slice(lastLastIndex));
	    }

	    return output.length > limit ? output.slice(0, limit) : output;
	}

}

// For browsers that don't have indexOf()
if(!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (obj, fromIndex) {
        if (fromIndex == null) {
            fromIndex = 0;
        } else if (fromIndex < 0) {
            fromIndex = Math.max(0, this.length + fromIndex);
        }
        for (var i = fromIndex, j = this.length; i < j; i++) {
            if (this[i] === obj)
                return i;
        }
        return -1;
    };
}

$.YQL = function(query, callback) { 
    if (!query || !callback) {
        throw new Error('$.YQL(): Parameters may be undefined');
    }
 
    var encodedQuery = encodeURIComponent(query.toLowerCase()),
        url = 'http://query.yahooapis.com/v1/public/yql?q='
            + encodedQuery + '&format=json&callback=?';
 
    $.getJSON(url, callback); 
};

$.fn.outerHTML = function(){
 
    // IE, Chrome & Safari will comply with the non-standard outerHTML, all others (FF) will have a fall-back for cloning
    return (!this.length) ? this : (this[0].outerHTML || (
      function(el){
          var div = document.createElement('div');
          div.appendChild(el.cloneNode(true));
          var contents = div.innerHTML;
          div = null;
          return contents;
    })(this[0]));
 
}